<template>
  <div class="main">
    <div class="form_content">
      <!-- <NScrollbar> -->
        <NSpin :show="spinShow">
          <div class="content">
            <div class="topTitle">征集信息</div>
            <NSpace>
              <NForm ref="formRef" :model="formValue" :rules="rules" label-placement="left" :label-width="115"
                label-align="right" disabled require-mark-placement="left">
                <NSpace>
                  <NFormItem label="姓名:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.userName" placeholder="请输入姓名" style="width: 250px" />
                  </NFormItem>
                  <!-- <NFormItem label="单位:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.dept" placeholder="请输入单位" style="width: 250px" />
                  </NFormItem> -->
                  <NFormItem label="联系方式:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.phone" placeholder="输入联系方式" style="width: 250px" />
                  </NFormItem>
                  <NFormItem label="证件类型:" path="meetingRoom">
                    <NSelect :options="listZOption" v-model:value="formValue.provideVo.idType" placeholder="请选择证件类型"
                      clearable style="width: 250px" />
                  </NFormItem>
                </NSpace>
                <NSpace>
                  <!-- <NFormItem label="证件类型:" path="meetingRoom">
                    <NSelect :options="listZOption" v-model:value="formValue.provideVo.idType" placeholder="请选择证件类型"
                      clearable style="width: 250px" />
                  </NFormItem> -->
                  <NFormItem label="证件号:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.idNo" placeholder="请输入证件号" style="width: 250px" />
                  </NFormItem>
                  <NFormItem label="提交时间:" path="meetingRoom">
                   <NDatePicker input-readonly v-model:formatted-value="formValue.provideVo.commitTime"
                      value-format="yyyy-MM-dd HH:mm:ss" type="datetime"  default-time="00:00:00" style="width: 250px" />
                  </NFormItem> 
                  <NFormItem label="征集品名称:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.collectName" placeholder="请输入征集品名称" style="width: 250px" />
                  </NFormItem>
                </NSpace>
                <NSpace>
                  <!-- <NFormItem label="征集品名称:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.collectName" placeholder="请输入征集品名称" style="width: 250px" />
                  </NFormItem> -->
                  <NFormItem label="征集品说明:" path="meetingRoom">
                    <NInput v-model:value="formValue.provideVo.collectNotes" placeholder="请输入征集品说明"
                      style="width: 250px" />
                  </NFormItem>
                  <NFormItem label="征集方式:" path="meetingRoom">
                    <NSelect :options="listOption" v-model:value="formValue.provideVo.collectMethodName"
                      placeholder="请选择征集方式" clearable style="width: 250px" />
                  </NFormItem>
                </NSpace>
                <NSpace>
                  <NFormItem label="附件:" path="meetingRoom">
                    <VAttachments :attach-list="formValue.provideVo.fileList"></VAttachments>
                  </NFormItem>
                </NSpace>
              </NForm>
            </NSpace>
            <div class="topTitle">征集审核</div>
            <NSpace>
              <NForm ref="formRef" :model="formValue" :rules="rules" label-placement="left" :label-width="115"
                label-align="right">
                <NSpace>
                  <NFormItem label="审核状态:" path="statusCode">
                    <NSelect :options="listStatusOption" v-model:value="formValue.examineVo.statusCode"
                      placeholder="请选择审核状态" clearable style="width: 250px" />
                  </NFormItem>
                  <div></div>
                </NSpace>
                <NSpace>
                  <NFormItem label="反馈内容:" path="feedbackContent">
                    <NInput v-model:value="formValue.examineVo.feedbackContent" placeholder="请输入反馈内容" style="width: 500px"
                      type="textarea" />
                  </NFormItem>
                  <div style="width: 480px"></div>
                </NSpace>
                <NSpace>
                  <NFormItem label="备注:" path="meetingRoom">
                    <NInput v-model:value="formValue.examineVo.feedbackNotes" placeholder="请输入备注" style="width: 500px"
                      type="textarea" />
                  </NFormItem>
                  <div style="width: 480px"></div>
                </NSpace>
              </NForm>
            </NSpace>
          </div>
        </NSpin>
      <!-- </NScrollbar> -->
    </div>
  </div>
  <FixedAction>
    <NSpace justify="center">
      <NButton style="width: 78px" ghost @click="goBack">
        取消
      </NButton>
      <NButton type="primary" style="width: 78px" @click="confirm">
        保存
      </NButton>
    </NSpace>
  </FixedAction>
</template>

<script setup lang="ts">
import { ref, watch, reactive } from 'vue'
import {
  NSpace,
  NForm,
  NInput,
  NFormItem,
  NButton,
  NScrollbar,
  NDatePicker,
  NSpin,
  useMessage,
  NSelect
} from 'naive-ui'
import type { FormInst } from 'naive-ui'
import router from '@/router'
import { useRoute } from 'vue-router'
import { useTagStore } from '@/stores/layout'
import FixedAction from '@/components/FixedAction.vue'
import { sendRequest } from '@/utils/api'
import type { HttpResponse } from '@/utils/http'
import { keysOf } from '@/utils/object'
import VAttachments from '@/views/OA/Common/Attachments.vue'

const tags = useTagStore().tags
const message = useMessage()
const route = useRoute()
const formRef = ref<FormInst | null>(null)
const listOption = ref([])
const listZOption = ref([])
const listStatusOption = ref([])
const formValue = reactive({
  examineVo: {
    feedbackContent: null,
    feedbackNotes: null,
    statusCode: null,
    statusName: null,
  },
  provideVo: {
    collectMethodName: null,
    collectName: null,
    collectNotes: null,
    commitTime: null,
    dept: null,
    feedbackNotes: null,
    fileIds: null,
    fileList: [],
    idNo: null,
    idType: null,
    phone: null,
    statusName: null,
    userName: null
  }
})
const rules = {
  statusCode: {
    required: true,
    trigger: ['blur', 'change'],
    validator: () => {
      if (!formValue.examineVo.statusCode) return new Error('请选择审核状态')
      return true
    }
  },
  feedbackContent: {
    required: true,
    trigger: ['blur', 'change'],
    validator: () => {
      if (!formValue.examineVo.feedbackContent) return new Error('请输入反馈内容')
      return true
    }
  }
}
//下拉
const getFunction = async () => {
  listOption.value = []
  const [error, res] = await sendRequest<HttpResponse>({
    module: 'collectNotice',
    api: 'listByParentCode',
    override: {
      params: {
        dicCode: 'A30'
      }
    }
  })
  if (error) return message.error(error.message)
  res.data.forEach((i: any) => {
    // @ts-ignore
    listOption.value.push({ label: i.dicName, value: i.dicCode })
  })
}
//状态下拉
const getSFunction = async () => {
  listStatusOption.value = []
  const [error, res] = await sendRequest<HttpResponse>({
    module: 'collectNotice',
    api: 'listByParentCode',
    override: {
      params: {
        dicCode: 'A38'
      }
    }
  })
  if (error) return message.error(error.message)
  res.data.forEach((i: any) => {
    if (i.dicName !== '待处理') {
      // @ts-ignore
      listStatusOption.value.push({ label: i.dicName, value: i.dicCode })
    }
  })
}
//证件类型下拉
const getZFunction = async () => {
  listZOption.value = []
  const [error, res] = await sendRequest<HttpResponse>({
    module: 'collectNotice',
    api: 'listByParentCode',
    override: {
      params: {
        dicCode: 'A37'
      }
    }
  })
  if (error) return message.error(error.message)
  res.data.forEach((i: any) => {
    // @ts-ignore
    listZOption.value.push({ label: i.dicName, value: i.dicCode })
  })
}
//回显
const spinShow = ref(false)
const idT = ref()
const getInfo = async () => {
  idT.value = route.params.id
  spinShow.value = true
  await sendRequest<HttpResponse>({
    module: 'collectNotice',
    api: 'infoProvide',
    override: {
      params: {
        id: idT.value
      }
    }
  }).then(([err, res]) => {
    if (err) return
    // keysOf(res.data.examineVo).forEach((e) => {
    //   {
    //     // @ts-ignore
    //     formValue.examineVo[e] = res.data.examineVo[e] as never
    //   }
    // })
    if(res.data.provideVo){
      keysOf(res.data.provideVo).forEach((e) => {
      {
        // @ts-ignore
        formValue.provideVo[e] = res.data.provideVo[e] as never
      }
    })
    }
  })
  spinShow.value = false
}

// 返回
const goBack = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)
  
    tags.splice(index, 1)
if (tags.length == 0) {
    router.push({ path: '/OA/Workbench/UserDesktop' })
  } else history.back()
}

// 确定
const confirm = () => {
  formRef.value?.validate(async (err: any) => {
    if (err) return
    const [error, res] = await sendRequest<HttpResponse>({
      module: 'collectNotice',
      api: 'examineProvide',
      override: {
        data: {
          ...formValue.examineVo,
          id: route.params.id
        }
      }
    })
    if (error) return message.error(error.message)
    message.success(res.msg)
    goBack()
  })
}

watch(
  () => route,
  () => {
    if (route.name == 'informationSolicitationEdit') {
      formValue.examineVo.statusCode=null
      formValue.examineVo.feedbackContent=null
      formValue.examineVo.feedbackNotes=null
      getInfo()
      getFunction()
      getSFunction()
      getZFunction()
    }
  },
  { deep: true, immediate: true }
)
</script>

<style scoped lang="scss">
@mixin CommonFont {
  font-size: 16rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}

.commonWidth {
  width: 360rem;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .topTitle {
    width: 100%;
    height: 64rem;
    border-bottom: 1px solid #fff;
    padding: 20rem;
    margin-bottom: 20rem;
    @include CommonFont;
  }

  .form_content {
    width: 100%;
    height: 100%;
    padding: 20rem 20rem 0;

    .content {
      display: flex;
      align-items: center;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 18rem 33rem 120rem;
      overflow-y: scroll;
      background-color: #ebeceb;

      .scope {
        width: 360rem;
        height: 130rem;
        background: #e1e1e1;
        margin-bottom: 25rem;
      }
    }
  }
}

.content::-webkit-scrollbar {
  display: none;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
